package components.editor
{
	import UIFree.uif_component;
	
	import components.c_component;
	import components.c_control;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class _c_dragRectHandle extends c_control {
		private var _beforeSize : Point;		
		private var _rollp		: Point;
		private var _propResize	: Boolean;
		
		public function _c_dragRectHandle(parent:uif_component, a_data: Object, a_size: int, styleClass: Object) {
			super(parent, 0, 0, a_size, a_size, styleClass);
			_data 		= a_data;
			_propResize = false;
		}
		
		public function get dragRect(): c_dragRect {
			return parent.parent as c_dragRect;
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(MouseEvent.MOUSE_DOWN, doMouseDown);
			addEvent(uif_component.STARTDRAG, doStartDrag);
			addEvent(uif_component.ENDDRAG, doEndDrag);
			addEvent(KeyboardEvent.KEY_DOWN, doKeyDown, stage);
			addEvent(KeyboardEvent.KEY_UP, doKeyUp, stage);
		}
		
		protected function doMouseDown(e: MouseEvent): void {
			beginDrag(2);
		}
		
		public function doStartDrag(e: Event): void {
			_beforeSize = dragRect.size.clone();
		}
		
		public function doEndDrag(e: Event): void {
			dragRect.saveHistory();
		}
		
		protected function doKeyDown(e: KeyboardEvent): void {
			if ((e.keyCode == 16) && !_propResize) {
/*				if (dragState) {
					var rect: Rectangle = dragRect.getOuter();
					var s: Point 		= _beforeSize.clone();
					s.normalize(rect.size.length);
					switch (_data) {
						case 1: rect.y += rect.size.y - s.y;
							break;
						case 5: rect.x += rect.size.x - s.x;
							break;
					}
					rect.size = s;
					dragRect.setRect(rect);
				}*/
				_propResize 	= true;
			}
		}
		
		protected function doKeyUp(e: KeyboardEvent): void {
			if ((e.keyCode == 16) && _propResize) {
				_propResize 	= false;
			}
		}
		
		protected function get propResize(): Boolean {
			return _propResize;
		}
		
		protected function checkNewRect(rect: Rectangle): Boolean {
			return (rect.right - rect.left >= c_dragRect.MINSIZE) && (rect.bottom - rect.top >= c_dragRect.MINSIZE);
		}
		
		public function get modity(): Object {
			return  {
				MR0: function (rect: Rectangle, n_x: Number, n_y: Number): Boolean {
					rect.top 	= n_y + height / 2;
					return rectUpdate(rect);
				},
				
				MR1: function (rect: Rectangle, n_x: Number, n_y: Number): Boolean {
					if (propResize) {
						var p: Point = new Point(n_x + width / 2, rect.height - n_y - height / 2);
						var s: Point = rect.size.clone();
						s.normalize(p.length);
						rect.right 	= s.x;
						rect.top 	= rect.height - s.y;
					} else {
						rect.right 	= n_x + width / 2;
						rect.top 	= n_y + height / 2;
					}
					return rectUpdate(rect);
				},
				
				MR2: function (rect: Rectangle, n_x: Number, n_y: Number): Boolean {
					rect.right = n_x + width / 2;
					return rectUpdate(rect);
				},
				
				MR3: function (rect: Rectangle, n_x: Number, n_y: Number): Boolean {
					var p: Point = new Point(n_x + width / 2, n_y + height / 2);
					if (propResize) {
						var s: Point = rect.size.clone();
						s.normalize(p.length);
						rect.right 	= s.x;
						rect.bottom	= s.y;
					} else {
						rect.right 	= p.x;
						rect.bottom	= p.y;
					}
					return rectUpdate(rect);
				},
				
				MR4: function (rect: Rectangle, n_x: Number, n_y: Number): Boolean {
					rect.bottom = n_y + height / 2;
					return rectUpdate(rect);
				},
				
				MR5: function (rect: Rectangle, n_x: Number, n_y: Number): Boolean {
					var p: Point;
					if (propResize) {
						p = new Point(rect.width - n_x - width / 2, n_y + height / 2);
						var s: Point = rect.size.clone();
						s.normalize(p.length);
						rect.left 	= rect.width - s.x;
						rect.bottom	= s.y;
					} else {
						p = new Point(n_x + width / 2, n_y + height / 2);
						rect.left 	= p.x;
						rect.bottom	= p.y;
					}
					return rectUpdate(rect);
				},
				
				MR6: function (rect: Rectangle, n_x: Number, n_y: Number): Boolean {
					rect.left	= n_x + width / 2;
					return rectUpdate(rect);
				},
				
				MR7: function (rect: Rectangle, n_x: Number, n_y: Number): Boolean {
					var p: Point;
					if (propResize) {
						p = new Point(rect.width - n_x - width / 2, rect.height - n_y - height / 2);
						var s: Point = rect.size.clone();
						s.normalize(p.length);
						rect.left 	= rect.width - s.x;
						rect.top	= rect.height - s.y;
					} else {
						p = new Point(n_x + width / 2, n_y + height / 2);
						rect.left 	= p.x;
						rect.top	= p.y;
					}
					return rectUpdate(rect);
				},
				
				MRROLL: function (rect: Rectangle, n_x: Number, n_y: Number): Boolean {
					var c	: Point 	= dragRect.center;
					
					var v1: Point 	= new Point(x - c.x, y - c.y); 
					var v2: Point 	= new Point(n_x - c.x, n_y - c.y);
					
					var angle: Number = Math.atan2(v1.x, v1.y) - Math.atan2(v2.x, v2.y);
					dragRect.rollMatrix(angle);
					return false;
				}
			}
		}
		
		protected function rectUpdate(rect: Rectangle): Boolean {
			if (checkNewRect(rect)) {
				dragRect.position = dragRect.transform.matrix.transformPoint(rect.topLeft);
				dragRect.resize(rect.width, rect.height);
				return true;
			} else return false;
		}
		
		protected function updateDragRect(n_x: Number, n_y: Number): Boolean {
			var method	: String = 'MR' + _data;
			if (modity.hasOwnProperty(method)) {
				return modity[method](new Rectangle(0, 0, dragRect.width, dragRect.height), n_x, n_y);
			} else return false;
		}
		
		override protected function updateDragPosition(n_x: Number, n_y: Number): Boolean {
			return updateDragRect(n_x, n_y);
		}
		
	}
}